package mo.tags.stack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class StockSpanner {

    private Stack<Integer> stack = new Stack<>();
    private List<Integer> arr = new ArrayList<>();
    private int idx = 0;

    public StockSpanner() {

    }

    public int next(int price) {
        arr.add(price);
        if (stack.isEmpty() || arr.get(stack.peek()) > price) {
            stack.push(idx);
            idx++;
            return 1;
        } else {
            while (!stack.isEmpty() && arr.get(stack.peek()) <= price) {
                stack.pop();
            }
            int startIdx = -1;
            if (!stack.isEmpty()) {
                startIdx = stack.peek();
            }
            stack.push(idx);
            idx++;
            return stack.peek() - startIdx;
        }
    }


    //["StockSpanner","next","next","next","next","next"]
    //[[],[31],[41],[48],[59],[79]]
    public static void main(String[] args) {
        StockSpanner spanner = new StockSpanner();
        System.out.println(spanner.next(100));
        System.out.println(spanner.next(80));
        System.out.println(spanner.next(60));
        System.out.println(spanner.next(70));
        System.out.println(spanner.next(60));
        System.out.println(spanner.next(75));
        System.out.println(spanner.next(85));
    }
}
